import Vue from 'vue'
import App from './App.vue'
import router from './router'
import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
import qs from 'qs'
import {
  sessionGet,
  sessionSet,
  sessionDestroy
} from './util.js'

Vue.config.productionTip = false
axios.defaults.withCredentials = true
//设置axios的基础url部分
axios.defaults.baseURL = 'http://localhost:8080/elm_war_exploded/';
//将axios挂载到vue上，使用是就可以 this.$axios 这样使用了
Vue.prototype.$axios = axios;
Vue.prototype.$qs = qs;
Vue.prototype.$sessionSet = sessionSet;
Vue.prototype.$sessionGet = sessionGet;


// 响应拦截器
axios.interceptors.response.use(function (response) {
  // console.log('对响应成功做点什么');
  let result = response.data;
  // console.log("响应结果:"+result);
  // 对返回的响应做出拦截,如果返回的是not log(未登录),则路由跳转到index首页
  if (result == 'not log') {
    console.log("响应拦截器:not log 已触发");
    sessionDestroy();
    // 非登录状态发起的登录态请求 会被拦截并 回到首页
    router.push('/index');
  }
  return response;
}, function (error) {
  console.log('对响应错误做点什么');
  return Promise.reject(error);
});

// 路由守卫者
router.beforeEach(function (to, from, next) {
  let user = sessionGet();
  //除了登陆、注册、首页、商家列表、商家信息之外，都需要判断是否登陆了
  if (!(to.path == '/' || to.path == '/index' || to.path == '/businessList' || to.path == '/businessInfo' || to.path ==
      '/login' || to.path == '/register')) {
        console.log(to.path);
    if (user == null) {
      router.push('/login');
      location.reload();
      console.log("user==null");
    }else{
      next();
    }
  }else{
    next();
  }
});

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')